package java_thinking.unit_11._11_13;


import java_thinking.unit_8.newshape.Circle;
import java_thinking.unit_8.newshape.Shape;
import java_thinking.unit_8.newshape.Square;
import java_thinking.unit_8.newshape.Triangle;

import java.util.Iterator;
import java.util.Random;

/**
 * @Description : e31
 * @author: cww
 * @DateTime: 2019-03-19 21:11
 */

public class RandomShapeGenerator implements Iterable<Shape>{

    private Random random = new Random(47);
    private final int quantity;
    RandomShapeGenerator(int quantity){
        this.quantity = quantity;
    }
    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<Shape> iterator() {
        return new Iterator<Shape>() {
            private int count;
            @Override
            public boolean hasNext() {
                return count<quantity;
            }

            @Override
            public Shape next() {
                ++count;
                return nextShape();
            }
        };
    }
    private Shape nextShape() {
        switch(random.nextInt(3)) {
            default:
            case 0: return new Square();
            case 1: return new Circle();
            case 2: return new Triangle();
        }
    }

    public static void main(String[] args) {
        RandomShapeGenerator rsg = new RandomShapeGenerator(10);
        for(Shape shape : rsg) {
            System.out.println(shape.getClass().getSimpleName());
        }
    }

}
